// pages/Goods/Comment/index.js
var common = require("../../../public.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemId: '',
    commnet: [],
    showComment: false,
    commentPlaceHolder: '我来说几句',
    commentValue: '',
    inputFocus: false,
    pageNum: 1,
    isloadingBusy: false,
    loadFinished: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    this.setData({
      itemId: options.itemId
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid
        })
        // 获取商品评论
        that.getCommentList()
      },
      fail: function (res) {
        common.showLoginPage()
      }
    })
  },
  //获取评论列表
  getCommentList() {
    this.loadMore(true)
  },
  //点击回复
  clickRely(e) {
    let {
      item
    } = e.currentTarget.dataset
    let that = this
    console.log(item)
    this.setData({
      showComment: true,
      commentPlaceHolder: '@' + item.commentUserName + ':',
      commentId: item.id
    }, function () {
      setTimeout(function () {
        that.setData({
          inputFocus: true
        })
      }, 500)
    })
  },
  //输入评论
  changeCommnet(e) {
    console.log(e)
    let {
      value
    } = e.detail
    this.setData({
      commentValue: value
    })
  },
  // 发表评论
  saveComment() {
    if (this.data.commentValue === null || this.data.commentValue.length === 0) {
      common.showToast('您没有输入任何文字')
      return
    }
    let that = this
    wx.cloud.callFunction({
      name: 'ContentCheck',
      data: {
        txt: that.data.commentValue
      },
      success(res) {
        if (res.result.errCode == 87014) {
          common.showToast('文字违规，请重新输入')
        } else {
          // 合规
          let tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token,
            pubUserId: that.data.userInfo.id,
            commentId: that.data.commentId,
            content: that.data.commentValue,
          }
          common.post("/storeArticle/saveStoreArticleCommentChat", tempJson, function (res) {
            console.log(res)
            if (res.status == 200) {
              that.onCommentClose()
              that.getCommentList()
              common.showToast('评论成功', 'success')
            } else {
              common.showRequestErrMsg(res)
            }
          })
        }
      },
      fail(err) {
        common.showToast('文字违规，请重新输入')
      }
    })
  },
  onCommentClose() { // 关闭模态框
    this.setData({
      commentId: ' ',
      commentValue: ' ',
      showComment: false,
    })
  },
  praiseAction(e) {
    const id = e.currentTarget.dataset.item.id
    const userPraiseFlag = e.currentTarget.dataset.item.userPraiseFlag
    if (userPraiseFlag) {
      this.cancelCommentPraise(id)
    } else {
      this.commitPraise(id)
    }
  },
  commitPraise(id) { //点赞
    const that = this
    const tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      commentId: id
    }
    common.post("/storeArticle/saveStoreArticleCommentPraise", tempJson, function (res) {
      if (res.status == 200) {
        that.getCommentList()
      }
    })
  },
  //取消点赞
  cancelCommentPraise(id) {
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      commentId: id
    }
    common.post("/storeArticle/cancleStoreArticleCommentPraise", tempJson, function (res) {
      if (res.status === 200) {
        that.getCommentList()
      }
    })
  },
  loadMore(refresh = false) {
    if (this.data.isloadingBusy === true || (!refresh && this.data.loadFinished === true)) {
      return
    }
    this.setData({
      isloadingBusy: true
    })

    if (refresh) {
      this.setData({
        loadFinished: false
      })
    }

    const pageNum = refresh ? 1 : this.data.pageNum + 1

    const that = this
    const tempJson = {
      articleId: that.data.itemId,
      userId: (that.data.userInfo && that.data.userInfo.id) ? that.data.userInfo.id : '',
      token: that.data.token ? that.data.token : '',
      pageNum
    }
    common.post('/storeArticle/getStoreArticleCommentList', tempJson, function (res) {
      if (res.status === 200) {
        const tempList = refresh ? res.data : [...that.data.commnet, ...res.data]
        const finshed = res.data.length < 10 ? true : false
        that.setData({
          commnet: tempList,
          isloadingBusy: false,
          loadFinished: finshed,
          pageNum
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  onReachBottom() {
    this.loadMore(false)
  },
})